package com.rd.student.jike.class2.xuezhixin;

import java.util.Collections;
import java.util.LinkedList;

public class CollectionsTest {
    public static void main(String[] args) {
        Student lhm = new Student("李宏鸣","计科",2,"描述",20);
        Student xzx = new Student("薛志鑫","计科",2,"描述",19);
        Student whx = new Student("王海鑫","计科",4,"描述",21);
        Student xyy = new Student("夏银银","计科",4,"描述",22);
        Student wxy = new Student("王向阳","计科",5,"描述",23);
        Student wjh = new Student("吴俊豪","计科",5,"描述",24);

        LinkedList list1 = new LinkedList();
        list1.add(lhm);
        list1.add(xzx);
        list1.add(whx);
        list1.add(xyy);
        list1.add(wxy);
        list1.add(wjh);

        System.out.println("从小到大");
        Collections.sort(list1);
        System.out.println(list1);
        System.out.println("从大到小");
        LinkedList list = reAge4(list1);
        System.out.println(list);
    }

    //从大到小
    public static LinkedList reAge4(LinkedList list){
        LinkedList list1 = new LinkedList();
        LinkedList list2 = new LinkedList();
        for(int i = 0;i<list.size();i++){
            Student student = (Student) list.get(i);
            list1.add(student.getAge());
        }
        for (int i = list1.size() -1;i >= 0;i--){
            int a = (int)list1.get(i);
            for(int j = 0;j<list.size();j++){
                Student student = (Student) list.get(j);
                if(student.getAge() == a){
                    list2.add(list.get(j));
                }
            }
        }
        return list2;
    }
}

class Student implements Comparable {
    private String name;
    private String major;
    private int cl;
    private String desc;
    private int age;

    public Student(String name, String major, int cl, String desc, int age) {
        this.name = name;
        this.major = major;
        this.cl = cl;
        this.desc = desc;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public String getMajor() {
        return major;
    }

    public int getCl() {
        return cl;
    }

    public String getDesc() {
        return desc;
    }

    public int getAge() {
        return age;
    }

    @Override
    public int compareTo(Object o) {
        Student student = (Student) o;
        if (this.age > student.age) {
            return 1;
        } else if (this.age < student.age) {
            return -1;
        }
        return 0;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", major='" + major + '\'' +
                ", cl=" + cl +
                ", desc='" + desc + '\'' +
                ", age=" + age +
                '}' + "\n";
    }
}
